<template>
    <div class="long-container">
        <filterComponent hideHead v-model:filter-data="filterData" showDownload @doSearch="handleSearch"
            @download="handleDownload" />
        <tableComponent class="set-height" theme="light" :table-header="tableHeader" :table-data="tableData"
            v-model:page-info="pageInfo" @pageChange="handlePageChange">
            <template #operate>
                <el-table-column width="70" label="操作" fixed="right">
                    <template #default="scope">
                        <el-button type="danger" :disabled="scope.row.status === 1" link @click="handleRefuse(scope.row)">拒保</el-button>
                    </template>
                </el-table-column>
            </template>
        </tableComponent>
    </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import filterComponent from '@/components/filterComponent/index.vue'
import tableComponent from '@/components/tableComponent/index.vue'
import { coClaimDailyInsur, exportCoClaimDailyInsur } from '@/api/tools/compensateRate.ts'
import { datinsureadminsetapi } from '@/api/api.ts'
import baseurl from '@/api/env'

const filterData = ref({
    list: {
        companyName: {
            name: '劳务公司',
            prop: 'companyName',
            value: '',
            placeholder: '请输入劳务公司',
            type: 'input',
            inputType: 'text',
            handleFocus: (item) => {

            },
            handleChange: (item) => {

            },
            handleInput: (item) => {

            }
        },
        claimRateQuery: {
            name: '理赔状态',
            prop: 'claimRateQuery',
            type: 'radiogroup',
            span: 24,
            value: '',
            options: [
                {
                    label: '全部',
                    value: ''
                },
                {
                    label: '有理赔',
                    value: 1
                },
                {
                    label: '无理赔',
                    value: 2
                }
            ]
        },
        status: {
            name: '操作状态',
            prop: 'status',
            type: 'radiogroup',
            span: 24,
            value: '',
            options: [
                {
                    label: '全部',
                    value: ''
                },
                {
                    label: '正常',
                    value: 0
                },
                {
                    label: '已拒保',
                    value: 1
                }
            ]
        }
    },
    buttonList: [
        {
            icon: 'upload',
            name: '导出列表',
            click: () => {
                handleDownload()
            }
        }
    ]
})

const pageInfo = ref({
    pageNum: 1,
    pageSize: 10,
    total: 0
})

const tableData = ref([])

const tableHeader = ref([
    {
        label: '劳务公司',
        prop: 'companyName',
    },
    {
        label: '所属商务',
        prop: 'saleName',
    },
    {
        label: '在保人数',
        prop: 'staffCount',
    },
    {
        label: '案件总数',
        prop: 'caseAmount',
    },
    {
        label: '结案金额',
        prop: 'finalMoney',
    },
    {
        label: '实际保费',
        prop: 'totalPrice',
    },
    {
        label: '理赔率',
        prop: 'claimRate',
    },
    {
        label: '操作',
        prop: 'operate'
    }
])

const handleSearch = () => {
    pageInfo.value.pageNum = 1
    getList()
}

const handleDownload = () => {
    const query = createQuery()
    query.insureCompanyType = 'insure002_gsc'
    exportCoClaimDailyInsur(query).then(res => {
        window.location.href = baseurl + '/common/download?delete=true&fileName=' + res
    })
}

const handlePageChange = (ev) => {
    pageInfo.value.pageNum = ev.pageNum
    pageInfo.value.pageSize = ev.pageSize
    getList()
}

const createQuery = () => {
    const data = {}
    for (const key in filterData.value.list) {
        if (typeof filterData.value.list[key].value === 'string') {
            filterData.value.list[key].value =
                filterData.value.list[key].value.trim()
        }
        data[filterData.value.list[key].prop] = filterData.value.list[key].value
    }
    return data
}

const getList = () => {
    const query = createQuery()
    query.insureCompanyType = 'insure002_gsc'
    query.pageNum = pageInfo.value.pageNum
    query.pageSize = pageInfo.value.pageSize
    coClaimDailyInsur(query).then(res => {
        tableData.value = res.rows || []
        pageInfo.value.total = res.total
    })
}

const handleRefuse = (row) => {
    ElMessageBox.confirm(`是否拒保 ${row.companyName}`, '提示', {
        type: 'error'
    }).then(() => {
        datinsureadminsetapi({
            companyId: row.companyId,
            refusalGuaranteeStatus: 1,
            blockedAccount: 1
        }).then(() => {
            getList()
        })
    })
}

onMounted(() => {
    getList()
})
</script>
<style lang="scss" scoped>
.container {
    padding: 0 30px;
}

.wrapper {
    background: #fff;
}
</style>